package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.MaterialMapper;
import com.ruoyi.system.mapper.PurchaseMapper;
import com.ruoyi.system.mapper.PurchaseRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.RequisitionsMapper;
import com.ruoyi.system.service.IRequisitionsService;

import javax.annotation.Resource;

/**
 * 请购单Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-04-30
 */
@Service
public class RequisitionsServiceImpl implements IRequisitionsService 
{
    @Autowired
    private RequisitionsMapper requisitionsMapper;

    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private PurchaseRecordMapper purchaseRecordMapper;

    @Resource
    private com.ruoyi.system.mapper.WatehouseMaterialMapper WatehouseMaterialMapper;

    @Resource
    private com.ruoyi.system.mapper.WarehouseDetailMapper WarehouseDetailMapper;

    @Resource
    private MaterialMapper materialMapper;
    /**
     * 查询请购单
     * 
     * @param rid 请购单ID
     * @return 请购单
     */
    @Override
    public Requisitions selectRequisitionsById(Long rid)
    {
        return requisitionsMapper.selectRequisitionsById(rid);
    }

    /**
     * 查询请购单列表
     * 
     * @param requisitions 请购单
     * @return 请购单
     */
    @Override
    public List<Requisitions> selectRequisitionsList(Requisitions requisitions)

    {
        List<Requisitions> requisitions1 = requisitionsMapper.selectRequisitionsList(requisitions);
        for (int i = 0; i < requisitions1.size(); i++) {
            if (requisitionsMapper.selectState(requisitions1.get(i).getRid())>0){
                      //证明有材料没生成
                requisitions1.get(i).setState(0);
            }else {
                //补偿接口
                if (requisitions1.get(i).getState()==0){
                    //更新状态
                    requisitionsMapper.updateState(requisitions1.get(i).getRid());
                }
                //全部生成完成了
                requisitions1.get(i).setState(1);

            }

        }

            return requisitions1;


    }

    //新增材料请购单
    @Override
    public int insertRequisitions(Requisitions requisitions)
    {
        System.out.println(requisitions);
        //先加大表，返回新插入的ID

        long id=requisitionsMapper.insertRequisitions(requisitions);
        if (id==0){
            return 0;
        }

        //循环插入小表
        List<ReqReocrd> reqReocrds = requisitions.getReqReocrds();
        for (int i = 0; i <reqReocrds.size(); i++) {
            //先判断材料是不是之前有的
//            MaterialThree material=new MaterialThree();
//            material.setmName(reqReocrds.get(i).getmName());
//            material.setmModel(reqReocrds.get(i).getmModel());
//            material.setmUnit(reqReocrds.get(i).getmUnit());
//            //查看当前材料有没有重复的
//            MaterialThree materialThree1 = WatehouseMaterialMapper.selectMatByname(material.getmName(), material.getmModel());
//            if (null == materialThree1) {
////            证明材料表中都没有这个材料
////            去材料表中添加新材料，并在关联表中插入新纪录
//                WatehouseMaterialMapper.insertMaterial(material);
//                //设置新的材料外键
//                reqReocrds.get(i).setmId(material.getmId());
//            }
            //证明这是个新添材料
            MaterialThree materialThree = new MaterialThree();
            materialThree.setmName(reqReocrds.get(i).getmName());
            materialThree.setmModel(reqReocrds.get(i).getmModel());
            materialThree.setmUnit(reqReocrds.get(i).getmUnit());
            Long mid=materialMapper.selectMId(materialThree);
            //查看材料表是否有这种材料
            if (null==mid) {
                //给材料表添加材料
                materialMapper.insertMatOnly(materialThree);
                reqReocrds.get(i).setmId(materialThree.getmId());
            }else {
                reqReocrds.get(i).setmId(mid);
            }
            ReqReocrd reqReocrd = reqReocrds.get(i);
            reqReocrd.setReqId(requisitions.getRid());
            if (requisitionsMapper.isnertRecord(reqReocrd)==0){
               return 0;
           }
        }
        //代表成功
       return 1;
    }

    /**
     * 修改请购单
     * 
     * @param requisitions 请购单
     * @return 结果
     */
    @Override
    public int updateRequisitions(Requisitions requisitions)
    {

        Long rid = requisitions.getRid();

        List<ReqReocrd> reqReocrds = requisitions.getReqReocrds();
        if (reqReocrds.size()>0) {
            for (int i = 0; i < reqReocrds.size(); i++) {
                if (null==reqReocrds.get(i).getId()){
                    //证明是新添材料
                    MaterialThree materialThree = new MaterialThree();
                    materialThree.setmName(reqReocrds.get(i).getmName());
                    materialThree.setmModel(reqReocrds.get(i).getmModel());
                    materialThree.setmUnit(reqReocrds.get(i).getmUnit());
                    Long mid=materialMapper.selectMId(materialThree);
                    //查看材料表是否有这种材料
                    if (null==mid) {
                        //给材料表添加材料
                        materialMapper.insertMatOnly(materialThree);
                        reqReocrds.get(i).setmId(materialThree.getmId());
                    }else {
                        reqReocrds.get(i).setmId(mid);
                    }
                    reqReocrds.get(i).setReqId(rid);
                    requisitionsMapper.isnertRecord(reqReocrds.get(i));
                }else {
                    //修改即可
                    requisitionsMapper.updateReqRec(reqReocrds.get(i));
                }

            }
        }
        return requisitionsMapper.updateRequisitions(requisitions);
    }

    /**
     * 批量删除请购单
     * 
     * @param rids 需要删除的请购单ID
     * @return 结果
     */
    @Override
    public int deleteRequisitionsByIds(Long[] rids)
    {
        for (int i = 0; i < rids.length; i++) {
            requisitionsMapper.deleteRequisitionsById(rids[i]);
            requisitionsMapper.deleteRecords(rids[i]);
        }
        return 1;
    }

    /**
     * 删除请购单信息
     * 
     * @param rid 请购单ID
     * @return 结果
     */
    @Override
    public int deleteRequisitionsById(Long rid)
    {
        return requisitionsMapper.deleteRequisitionsById(rid);
    }


    //请购单详情
    @Override
    public Requisitions listRequisition(long rid) {

        return requisitionsMapper.listRequisition(rid);
    }

    @Override
    public int deleteRecord(long rid) {

        return  requisitionsMapper.deleteRecord(rid);
    }

    //删除请购单详情
    @Override
    public int deleteRecords(long rid) {
        return requisitionsMapper.deleteRecords(rid);
    }


    //生成采购单
    @Override
    public int purchase(Requisitions requisitions) {

        //需要生成采购的材料
        List<ReqReocrd> reqReocrds1 = requisitions.getReqReocrds();
        //插入大表
        Purchase purchase = new Purchase();
        //项目名称
        purchase.setProName(requisitions.getProName());
        //项目编号
        purchase.setProNumber(requisitions.getProNumber());
        //交期要求
        purchase.setDeliveryReq(requisitions.getDeliveryReq());
        //申请日期
        purchase.setRequisitionDate(requisitions.getRequisitionDate());
        //插入Id
        purchase.setReqId(requisitions.getRid());
        //返回插入的ID
        purchaseMapper.insertPurchase(purchase);
        //去给外键赋值
        Long id = purchase.getId();
//        requisitions.setPurId(id);
//        requisitionsMapper.updateRequisitions(requisitions);
//        System.out.println(id);
        //插入材料详情表
        for (int i = 0; i <reqReocrds1.size(); i++) {
            MaterialThree materialThree = new MaterialThree();
            materialThree.setmName(reqReocrds1.get(i).getmName());
            materialThree.setmModel(reqReocrds1.get(i).getmModel());
            materialThree.setmUnit(reqReocrds1.get(i).getmUnit());
            Long mid=materialMapper.selectMId(materialThree);
            //查看材料表是否有这种材料
            if (null==mid) {
                //给材料表添加材料
                materialMapper.insertMatOnly(materialThree);
                reqReocrds1.get(i).setmId(materialThree.getmId());
            }else {
                reqReocrds1.get(i).setmId(mid);
            }
            PurchaseRecord purchaseRecord = new PurchaseRecord();
            //材料规格
            purchaseRecord.setMdSpecifications(reqReocrds1.get(i).getMdSpecifications());
            //材料外键
            purchaseRecord.setmId(reqReocrds1.get(i).getmId());
            //材料数量
            purchaseRecord.setMdSum(reqReocrds1.get(i).getMdSum());
            //备注
            purchaseRecord.setRemark(reqReocrds1.get(i).getRemark());
            //请购单外键
            purchaseRecord.setPurId(id);
            //请购单材料外键
            purchaseRecord.setReqRecId(reqReocrds1.get(i).getId());
            //修改状态
            requisitionsMapper.uodateState(reqReocrds1.get(i).getId());
            purchaseRecordMapper.insertPurchaseRecord(purchaseRecord);
        }
        return 1;
    }

    @Override
    public List<SelectPurchase> selectPurchase(Long rid) {
        return requisitionsMapper.selectPurchase(rid);
    }
}
